Exercise 2: Finding Hermite Curve Parameters

Find the values of the points P 0 and P 1 as the values of the derivatives at these endpoints to draw the four curves below:


In [1]:
from IPython.display import Image

Image(filename='img_ex_2.png')


Out[1]:

In [8]:
%matplotlib inline
import numpy
import numpy as np
import pylab
import matplotlib.pyplot as plt

class HermiteCurve:
    
    def __init__(self):
        self.p0 = np.array([0, 0])
        self.p1 = np.array([0, 0])
        self.p0_tangent = np.array([0, 0])
        self.p1_tangent = np.array([0, 0])
        self.plt = plt
        
        self.calculcate_coefficients()
        self.setup_plot()
        
    def setup_plot(self):
        self.plt.margins(0.1)
        self.plt.grid()
        
    def calculcate_coefficients(self):
        self.a = 2 * self.p0 - 2 * self.p1 + self.p0_tangent + self.p1_tangent
        self.b = -3 * self.p0 + 3 * self.p1 - 2 * self.p0_tangent - self.p1_tangent
        self.c = self.p0_tangent
        self.d = self.p0
        
    def calculate_coefficient_matrix(self):    
        coefficients = numpy.matrix(
            (
                ( 2, -2,  1,  1),
                (-3,  3, -2, -1),
                ( 0,  0,  1,  0),
                ( 1,  0,  0,  0)
            )
        )
        
        points = numpy.matrix(
            (
                (self.p0),
                (self.p1),
                (self.p0_tangent),
                (self.p1_tangent)
            )
        )
        
        return np.dot(coefficients, points)
        
    def calculate_curve_point(self, time):
        return self.a * np.power(time, 3) + self.b * np.power(time, 2) + self.c * time + self.d
        
    def taylor_f_10(self, x):
        return numpy.polyval([1./362880., 0, -1./5040., 0, 1./120., 0, -1./6., 0, 1., 0], x)
        
    def plot(self):
        # Plot points
        x = [self.p0[0], self.p1[0]]
        y = [self.p0[1], self.p1[1]]
        # Plot arrow
        plt.arrow(
            self.p0[0], 
            self.p0[1], 
            self.p0_tangent[0] - self.p0[0], 
            self.p0_tangent[1] - self.p0[1]
        )
        plt.arrow(
            self.p1[0],
            self.p1[1],
            self.p1_tangent[0] - self.p1[0],
            self.p1_tangent[1] - self.p1[1]
        )
        plt.plot(x, y, 'ro')
        
        # Prepare data
        self.calculcate_coefficients()
        coeff_matrix = self.calculate_coefficient_matrix()
        
        # Gather data
        plot_data = []
        for t in numpy.linspace(0, 1, 100):
            plot_data.append(
                numpy.dot(
                    numpy.matrix(
                        (
                            (numpy.power(t, 3)),
                            (numpy.power(t, 2)),
                            (t),
                            (1)
                        )
                    ),
                    coeff_matrix
                )
            )
        
        x = []
        y = []
        for data in plot_data:
            x.append(data.A[0][0])
            y.append(data.A[0][1])
            
        # Plot
        plt.plot(x, y)

In [9]:
# Just some test..

c = HermiteCurve()
# P0
c.p0 = np.array([0, 0])
c.p0_tangent = np.array([-0.25, 0.5])

# P1
c.p1 = np.array([1, 1])
c.p1_tangent = np.array([1.1, 0.2])

c.plot()



In [10]:
# Here we go.. 2.1
c = HermiteCurve()
# P0
c.p0 = np.array([0, 0])
c.p0_tangent = np.array([100.3, 1.2])

# P1
c.p1 = np.array([1, 0])
c.p1_tangent = np.array([100.3, -1.2])

c.plot()



In [11]:
# Here we go.. 2.3
c = HermiteCurve()
# P0
c.p0 = np.array([0, 1])
c.p0_tangent = np.array([3, 0.1])

# P1
c.p1 = np.array([1, 0])
c.p1_tangent = np.array([3, -0.2])

c.plot()



In [32]:
# Here we go.. 2.4
c = HermiteCurve()
# P0
c.p0 = np.array([0, 1])
c.p0_tangent = np.array([-3, 0.1])

# P1
c.p1 = np.array([1, 0])
c.p1_tangent = np.array([-3, -0.1])

c.plot()



In [ ]: